package com.catmiw.swt;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.catmiw.mp3.Mp3File;
import com.catmiw.wyz.util.DateTool;
import com.catmiw.wyz.util.ExcelUtils;
import org.apache.poi.ss.usermodel.Sheet;

public class DialogEnglishTest extends BasicDialog {

	public DialogEnglishTest(Shell parent) {
		super(parent);
		rowposition = new int[] { 14, 80, 6 };
		columnposition = new int[] { 40, 30, 30 };
	}

	private String books[];
	private Integer units[];
	private String mp3Dir;
	private String wordExcelFile;
	private String recordExcelFile;
	private String xmlfilename;
	private String paperDir;
	private String morphologyPaperModal;
	
	private Map<String,String> partOfSpeechTrans=new HashMap<String,String>();
	
	@Override
	public void setProperties(Properties properties) {
		this.properties = properties;
		mp3Dir = properties.getProperty("英语.mp3单词测试.文件目录");
		wordExcelFile = properties.getProperty("英语单词Excel文件名");
		recordExcelFile = properties.getProperty("英语.单词听写.测试记录文件");
		xmlfilename=properties.getProperty("英语单词文件名");
		
		paperDir=properties.getProperty("英语.试卷.目录");
		morphologyPaperModal=properties.getProperty("英语.词形变化试卷.模板");

		String t=properties.getProperty("英语.词性对照.名词");
		String tt[]=t.split(",");
		for (String ttt:tt){
			String tttt[]=ttt.split("~");
			if (tttt!=null&&tttt.length==2){
				partOfSpeechTrans.put(tttt[0],tttt[1]);
			}
		}
		t=properties.getProperty("英语.词性对照.动词");
		tt=t.split(",");
		for (String ttt:tt){
			String tttt[]=ttt.split("~");
			if (tttt!=null&&tttt.length==2){
				partOfSpeechTrans.put(tttt[0],tttt[1]);
			}
		}

		String bk = properties.getProperty("英语课本");
		String[] tp = bk.split(",");
		books = new String[tp.length];
		units = new Integer[tp.length];

		for (int i = 0; i < tp.length; i++) {
			String str = tp[i];
			String[] tx = str.split("-");
			books[i] = tx[0];
			units[i] = Integer.parseInt(tx[1]);
		}

		recordWorkbook = ExcelUtils.getWorkbook(recordExcelFile);
		loadWordsPriority();
		loadWordsList();
	}

	public void loadWordsPriority() {
		wordPriority.clear();
		Map<String,Integer> wrongList=new HashMap<String,Integer>();
		for (int i = 0; i < recordWorkbook.getNumberOfSheets(); i++) {
			Sheet sheet = recordWorkbook.getSheetAt(i);
			String sheetname = sheet.getSheetName();
			if (!sheetname.startsWith("test_"))
				continue;
			String namet[] = sheetname.split("_");
			Date date = DateTool.toDate(namet[1]);
			int days = DateTool.daysBetween(date, new Date());

			for (int j = 0; j < 65525; j++) {
				Row row = sheet.getRow(j);
				if (row == null)
					break;
				for (int k = 0; k < 100; k++) {
					Cell cell = row.getCell(k * 2);
					if (cell == null)
						break;
					String word = cell.getStringCellValue();
					if (word == null || "".equals(word.trim()))
						break;
					Integer c = wordPriority.get(word);
					if (c == null)
						c = 0;
					int d = days > 50 ? 50 : days;
					c -= (50 - d) * 2;

					cell = row.getCell(k * 2 + 1);
					if (cell != null
							&& !("".equals(cell.getStringCellValue().trim()))) {
						d = days > 25 ? 25 : days;
						c += d * 4;
						String tt=word.trim();
						Integer it=wrongList.get(tt);
						if (it==null)
							it=0;
						it++;
						wrongList.put(tt, it);
					}

					wordPriority.put(word.trim(), c);
				}
			}
		}
		
		System.out.println(wrongList);
	}

	private List<NewWord> newWordList = new ArrayList<NewWord>();

	public void loadWordsList() {
		newWordList.clear();
		Workbook wb = ExcelUtils.getWorkbook(wordExcelFile);
		Sheet sheet = wb.getSheet("单词");
		for (int i = 1; i < 65525; i++) {
			Row row = sheet.getRow(i);
			if (row == null)
				break;
			Cell cell = row.getCell(2);
			if (cell == null)
				break;
			String word = cell.getStringCellValue();
			if (word == null || "".equals(word.trim()))
				break;
			NewWord nw = new NewWord();
			nw.setWord(word.trim());

			cell = row.getCell(0);
			if (cell == null)
				break;
			String book = cell.getStringCellValue();
			nw.setBook(book);

			cell = row.getCell(1);
			if (cell == null)
				break;
			double unit = cell.getNumericCellValue();
			nw.setUnit((int) unit);

			cell = row.getCell(3);
			if (cell == null)
				break;
			double require = cell.getNumericCellValue();
			nw.setRequire((int) require);

			cell = row.getCell(4);
			if (cell == null)
				break;
			String means = cell.getStringCellValue();
			nw.setMeans(means);

			newWordList.add(nw);

			checkPriority(nw);
		}
	}

	private Map<String, Integer> wordPriority = new HashMap<String, Integer>();
	private Workbook recordWorkbook;

	public void checkPriority(NewWord word) {
		int priority = 100;
		Integer c = wordPriority.get(word.getWord());
		if (c == null)
			c = 0;
		word.setPriority((priority + c)*10000+RAND.nextInt(10000));
	}

	private Button[] btBooks;
	private Combo[] cmbUnitb;
	private Combo[] cmbUnite;
	private Composite cmpWordList;
	private Button[] btWords;
	private Label lbMessage;
	private Button btSelectAll;
	private Button btConfirm;
	private Button btQuit;
	private Text txCountLimit;
	private Text txReadTimes;
	private Button btMorphology;

	private Button[] btRequire;

	private static int COLUMNCOUNT = 10;

	public Composite createCommandButtons(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FormLayout());

		lbMessage = new Label(composite, SWT.NONE);

		FormData data = new FormData();
		data.left = new FormAttachment(0, COMPOSITESPACE);
		data.right = new FormAttachment(50, 0 - COMPOSITESPACE);
		lbMessage.setLayoutData(data);

		btMorphology=new Button(composite,SWT.NONE);
		btMorphology.setText("产生词形变化考卷");
		
		btSelectAll = new Button(composite, SWT.NONE);
		btSelectAll.setText("全选");

		btConfirm = new Button(composite, SWT.NONE);
		btConfirm.setText("生成");

		btQuit = new Button(composite, SWT.NONE);
		btQuit.setText("退出");

		data = new FormData();
		data.right = new FormAttachment(100, 0 - COMPOSITESPACE);
		btQuit.setLayoutData(data);

		data = new FormData();
		data.right = new FormAttachment(btQuit, 0 - COMPOSITESPACE);
		btConfirm.setLayoutData(data);

		data = new FormData();
		data.right = new FormAttachment(btConfirm, 0 - COMPOSITESPACE);
		btSelectAll.setLayoutData(data);

		data = new FormData();
		data.right = new FormAttachment(btSelectAll, 0 - COMPOSITESPACE);
		btMorphology.setLayoutData(data);

		return composite;
	}

	private String[] chooseBooks;
	private int[] chooseUnitb;
	private int[] chooseUnite;

	@Override
	public void createContents() {
		shell.setText("英语听写测试");
		shell.setLayout(new FormLayout());

		Group gpRange = new Group(shell, SWT.SHADOW_OUT);
		gpRange.setText("设置课本范围");
		gpRange.setLayout(new GridLayout(3, false));

		btBooks = new Button[books.length];
		cmbUnitb = new Combo[books.length];
		cmbUnite = new Combo[books.length];

		chooseBooks = new String[books.length];
		chooseUnitb = new int[books.length];
		chooseUnite = new int[books.length];

		for (int i = 0; i < books.length; i++) {
			btBooks[i] = new Button(gpRange, SWT.CHECK);
			btBooks[i].setText(books[i]);
			btBooks[i].setSelection(true);
			btBooks[i].setData(i);
			chooseBooks[i] = books[i];

			String[] unitoptions = new String[units[i]];
			for (int j = 0; j < units[i]; j++)
				unitoptions[j] = "" + (1 + j);
			cmbUnitb[i] = createComboChooser(gpRange, "单元：", SWT.READ_ONLY,
					unitoptions, "");
			cmbUnitb[i].setText("1");
			cmbUnitb[i].setData(i);
			chooseUnitb[i] = 1;

			cmbUnite[i] = createComboChooser(gpRange, "~", SWT.READ_ONLY,
					unitoptions, "");
			cmbUnite[i].setText("" + units[i]);
			cmbUnite[i].setData(i);
			chooseUnite[i] = units[i];
		}
		setPosition(gpRange, 1, 1);

		Group gpRequire = new Group(shell, SWT.SHADOW_OUT);
		gpRequire.setText("设置单词要求范围");
		gpRequire.setLayout(new GridLayout(1, false));
		btRequire = new Button[2];

		btRequire[0] = new Button(gpRequire, SWT.RADIO);
		btRequire[0].setText("三会单词");
		btRequire[0].setData(3);
		btRequire[1] = new Button(gpRequire, SWT.RADIO);
		btRequire[1].setText("四会单词");
		btRequire[1].setSelection(true);
		btRequire[1].setData(4);
		chooseRequire=4;

		setPosition(gpRequire, 1, 2);

		Group gpOtherSet = new Group(shell, SWT.SHADOW_OUT);
		gpOtherSet.setText("其他设置");
		gpOtherSet.setLayout(new GridLayout(2, false));

		Label lbMax = new Label(gpOtherSet, SWT.NULL);
		lbMax.setText("选词数量限制：");
		txCountLimit = new Text(gpOtherSet, SWT.BORDER);
		txCountLimit.setText(" 40");

		Label lbReadTimes = new Label(gpOtherSet, SWT.NULL);
		lbReadTimes.setText("单词重复次数：");
		txReadTimes = new Text(gpOtherSet, SWT.BORDER);
		txReadTimes.setText("  3");

		setPosition(gpOtherSet, 1, 3);

		ScrolledComposite sc = new ScrolledComposite(shell, SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER);
		cmpWordList = new Composite(sc, SWT.NONE);
		sc.setContent(cmpWordList);
		cmpWordList.setLayout(new GridLayout(COLUMNCOUNT, false));
		cmpWordList.setSize(cmpWordList.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		cmpWordList.layout();
		setPosition(sc, 2);

		Composite commandbuttons = createCommandButtons(shell);
		setPosition(commandbuttons, 3);

		Point size = gpRange.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		shell.setSize(900, size.y * 8 + 60);

		shell.setLocation(
				Display.getCurrent().getClientArea().width / 2
						- shell.getSize().x / 2,
				Display.getCurrent().getClientArea().height / 2
						- shell.getSize().y / 2);
		
		wordRangeChanged();
	}

	private int chooseRequire;

	private List<NewWord> rangedWord = new ArrayList<NewWord>();

	public void wordRangeChanged() {
		rangedWord.clear();
		for (NewWord word : newWordList) {
			for (int i = 0; i < chooseBooks.length; i++) {
				String s = chooseBooks[i];
				if (s.equals(word.getBook())
						&& chooseUnitb[i] <= word.getUnit()
						&& chooseUnite[i] >= word.getUnit()
						&& word.getRequire() >= chooseRequire) {
					rangedWord.add(word);
				}
			}
		}
		clearWordsTable();
		
		Collections.sort(rangedWord,new Comparator<NewWord>(){
			@Override
			public int compare(NewWord o1, NewWord o2) {
				return o2.getPriority()-o1.getPriority();
			}
		});
		
		btWords=new Button[rangedWord.size()];
		int count=0;
		for (NewWord word:rangedWord){
			btWords[count]=new Button(cmpWordList,SWT.CHECK);
			btWords[count].setData(word.getWord());
			btWords[count].addSelectionListener(new SelectionAdapter(){
				public void widgetSelected(SelectionEvent e) {
					String t=e.getSource().toString();
					
					t=t.substring(t.indexOf('{')+1,t.indexOf('}'));
					wordSelected(t);
				}
			});

			btWords[count++].setText(""+word.getWord());
		}
		cmpWordList.setSize(cmpWordList.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		cmpWordList.layout();
	}

	public void wordSelected(String c){
		int limit=Integer.parseInt(txCountLimit.getText().trim());
		if (chooseWordList.contains(c)){
			chooseWordList.remove(chooseWordList.indexOf(c));
		} else {
			if (limit>chooseWordList.size()){
				chooseWordList.add(c);
			} else {
				messagebox("提示","选中的单词超过限制！");
			}
		}
		
		lbMessage.setText("共选中[ "+chooseWordList.size()+"/"+limit+" ]个字.");
	}
	
	public void clearWordsTable(){
		if (btWords==null)
			return;
		
		for (int i=0;i<btWords.length;i++){
			btWords[i].dispose();
			btWords[i]=null;
		}
		btWords=null;
		chooseWordList.clear();
		lbMessage.setText("");
	
	}
	
	@Override
	public void addListeners() {
		for (final Button button : btRequire) {
			button.addSelectionListener(new SelectionListener() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					Button b = (Button) e.getSource();
					if (b.getSelection() == true) {
						Integer i = (Integer) b.getData();
						chooseRequire = i.intValue();
						wordRangeChanged();
					}
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) {
				}
			});
		}
		for (final Button button : btBooks) {
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Integer i = (Integer) button.getData();
					if (button.getSelection()) {
						chooseBooks[i] = button.getText();
					} else {
						chooseBooks[i] = "";
					}
					wordRangeChanged();
				}
			});
		}
		for (final Combo cbx : cmbUnitb) {
			cbx.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Integer i = (Integer) cbx.getData();
					chooseUnitb[i] = Integer.parseInt(cbx.getText());
					wordRangeChanged();
				}
			});
		}
		for (final Combo cbx : cmbUnite) {
			cbx.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Integer i = (Integer) cbx.getData();
					chooseUnite[i] = Integer.parseInt(cbx.getText());
					wordRangeChanged();
				}
			});
		}
		btQuit.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				shell.dispose();
			}
		});

		btConfirm.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				String rtn = createMp3File();
				if (rtn != null) {
					messagebox("错误", rtn);
				} else {
					messagebox("提示", "生成MP3文件完成！");
				}
			}
		});

		btSelectAll.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				selectAll();
			}
		});
		
		btMorphology.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				morphologyPaper();
			}
		});
	}

	private static Random RAND = new Random();

	public String createMp3File() {
		List<String> words = new ArrayList<String>();
		List<String> wordsback = new ArrayList<String>();
		for (String str:chooseWordList){
			wordsback.add(str);
		}
		int count = wordsback.size();
		for (int i = 0; i < count; i++) {
			int rand = RAND.nextInt(wordsback.size());
			words.add(wordsback.get(rand));
			wordsback.remove(rand);
		}

		String today = new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date());
		String filename = mp3Dir + today + "[" + words.size() + "words]";
		Mp3File.createEnglishWordTestMp3(words, filename + ".mp3");

		writeRecordFile(today, words);
		return null;
	}

	private void writeRecordFile(String sheetname, List<String> words) {
		Sheet sheet = recordWorkbook.createSheet("test_" + sheetname);
		recordWorkbook.setSheetOrder(sheet.getSheetName(), 0);
		recordWorkbook.setActiveSheet(0);

		int rownum = 0;
		Row row = null;
		for (int i = 0; i < words.size(); i++) {
			int mod = i % 4;
			if (mod == 0)
				row = sheet.createRow(rownum++);
			Cell cell = row.createCell(mod * 2);
			cell.setCellValue(words.get(i));
		}
		ExcelUtils.writeWorkbook(recordWorkbook, recordExcelFile);
	}

	private List<String> chooseWordList = new ArrayList<String>();
	private int maxWords;

	public void selectAll() {
		chooseWordList.clear();
		maxWords = Integer.parseInt(txCountLimit.getText().trim());
		for (Button b : btWords) {
			if (b.getSelection()) {
				b.setSelection(false);
			} else {
				if (chooseWordList.size()<maxWords){
					b.setSelection(true);
					chooseWordList.add(b.getText());
				}
			}
		}
		lbMessage.setText("共选中[ " + chooseWordList.size() + "/" + maxWords
				+ " ]个单词.");

	}
	
	public void morphologyPaper(){
		List<String> ncount=new ArrayList<String>();
		List<String> ncountabnormal=new ArrayList<String>();
		
		List<String> verbsingle=new ArrayList<String>();
		List<String> verbsingleabnormal=new ArrayList<String>();
		
		List<String> verbpresent=new ArrayList<String>();
		List<String> verbpresentabnormal=new ArrayList<String>();
		
		Map<String,String> wordmap=new HashMap<String,String>();
		EnglishWordsXmlHandler xmlhandler=new EnglishWordsXmlHandler(xmlfilename);
		for(NewWord word:newWordList){
			String[] means=word.getMeans().split(",");
			Word w=xmlhandler.loadWord(word.getWord());
			//System.out.println(w.getId()+"\t"+w.getPlural()+"====");
			if (w!=null){
				for (String m:means){
					String pos=w.getMeanings().get(Integer.parseInt(m)-1).getPartOfSpeech();
					String t=wordmap.get(pos);
					if (t==null){
						wordmap.put(pos, word.getWord());
					} else {
						wordmap.put(pos,t+","+word.getWord());
					}
					String tx=partOfSpeechTrans.get(pos);
					if(tx!=null&&"名词(可数)".equals(tx)){
						if (!((w.getId()+"s").equals(w.getPlural()))){
							if (!ncountabnormal.contains(w.getId()))
								ncountabnormal.add(w.getId());
						} else {
							if (!ncount.contains(w.getId()))
								ncount.add(w.getId());
						}
					}
					if(tx!=null&&"动词".equals(tx)){
						if (!((w.getId()+"s").equals(w.getThirdPersonSingular()))){
							if (!verbsingleabnormal.contains(w.getId()))
								verbsingleabnormal.add(w.getId());
						} else {
							if (!verbsingle.contains(w.getId()))
								verbsingle.add(w.getId());
						}
						if (!((w.getId()+"ing").equals(w.getPresentParticiple()))){
							if (!verbpresentabnormal.contains(w.getId()))
								verbpresentabnormal.add(w.getId());
						} else {
							if (!verbpresent.contains(w.getId()))
								verbpresent.add(w.getId());
						}
					}
				}
			}
		}
		
		List<String> test1=new ArrayList<String>();
		for (int i=0;i<10;i++){
			int rand=RAND.nextInt(ncountabnormal.size());
			test1.add(ncountabnormal.get(rand));
			ncountabnormal.remove(rand);
		}
		for (int i=0;i<10;i++){
			int rand=RAND.nextInt(ncount.size());
			test1.add(ncount.get(rand));
			ncount.remove(rand);
		}
		
		List<String> test2=new ArrayList<String>();
		for (int i=0;i<5;i++){
			int rand=RAND.nextInt(verbsingleabnormal.size());
			test2.add(verbsingleabnormal.get(rand));
			verbsingleabnormal.remove(rand);
		}
		for (int i=0;i<5;i++){
			int rand=RAND.nextInt(verbsingle.size());
			test2.add(verbsingle.get(rand));
			verbsingle.remove(rand);
		}
		
		List<String> test3=new ArrayList<String>();
		for (int i=0;i<5;i++){
			int rand=RAND.nextInt(verbpresentabnormal.size());
			test3.add(verbpresentabnormal.get(rand));
			verbpresentabnormal.remove(rand);
		}
		for (int i=0;i<5;i++){
			int rand=RAND.nextInt(verbpresent.size());
			test3.add(verbpresent.get(rand));
			verbpresent.remove(rand);
		}
		Comparator<String> comparator=new Comparator<String>(){
			@Override
			public int compare(String arg0, String arg1) {
				return RAND.nextInt(10000)-RAND.nextInt(10000);
			}
		};
		Collections.sort(test1,comparator);
		Collections.sort(test2,comparator);
		Collections.sort(test3,comparator);
		
		Workbook wb=ExcelUtils.getWorkbook(morphologyPaperModal);
		Sheet sheet=wb.getSheetAt(0);
		int rownum=0;
		Row row=null;
		for (int i=0;i<test1.size();i++ ){
			int mod=i%5;
			if (mod==0){
				rownum+=2;
				row=sheet.getRow(rownum);
			}
			Cell cell=row.getCell(mod);
			cell.setCellValue(test1.get(i));
		}
		rownum++;
		for (int i=0;i<test2.size();i++ ){
			int mod=i%5;
			if (mod==0){
				rownum+=2;
				row=sheet.getRow(rownum);
			}
			Cell cell=row.getCell(mod);
			cell.setCellValue(test2.get(i));
		}
		rownum++;
		for (int i=0;i<test3.size();i++ ){
			int mod=i%5;
			if (mod==0){
				rownum+=2;
				row=sheet.getRow(rownum);
			}
			Cell cell=row.getCell(mod);
			cell.setCellValue(test3.get(i));
		}
		
		String today=DateTool.dateToString(new Date(), "yyyy-MM-dd");
		ExcelUtils.writeWorkbook(wb, paperDir+today+".xls");
		
/*		for(String gs:wordmap.keySet()){
			//System.out.println(gs+":"+wordmap.get(gs));
		}
		System.out.println(ncount.size()+":"+ncount);
		System.out.println(ncountabnormal.size()+":"+ncountabnormal);
		
		System.out.println(verbsingle.size()+":"+verbsingle);
		System.out.println(verbsingleabnormal.size()+":"+verbsingleabnormal);
		
		System.out.println(verbpresent.size()+":"+verbpresent);
		System.out.println(verbpresentabnormal.size()+":"+verbpresentabnormal);*/
		
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		DialogEnglishTest dc = new DialogEnglishTest(shell);
		Properties p = new Properties();
		try {
			p.load(new FileReader(
					"d:/studyassist/config/studyassist.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		dc.setProperties(p);

		dc.open();
	}

}
